package io.rong.imkit.myUI.custom_message;

import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableString;

import android.text.SpannableStringBuilder;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import io.rong.imkit.RongContext;
import io.rong.imkit.RongIM;
import io.rong.imkit.emoticon.AndroidEmoji;
import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.widget.AutoLinkTextView;
import io.rong.imkit.widget.ILinkClickListener;
import io.rong.imkit.widget.LinkTextViewMovementMethod;
import io.rong.imkit.widget.provider.IContainerItemProvider;
import io.rong.imlib.model.Message;

import io.rong.imlib.model.MessageContent;


@ProviderTag(
        messageContent = MyTextMessage.class,
        showReadState = true
)
public class CustomizeMessage extends IContainerItemProvider.MessageProvider<MyTextMessage> {
    private static final String TAG = "CustomizeMessage";

    private SpannableStringBuilder textMessageContent;


    public CustomizeMessage() {
    }

    public View newView(Context context, ViewGroup group) {
        View view = LayoutInflater.from(context).inflate(io.rong.imkit.R.layout.rc_item_text_message, (ViewGroup)null);
        CustomizeMessage.ViewHolder holder = new CustomizeMessage.ViewHolder();
        holder.message = (AutoLinkTextView)view.findViewById(16908308);
        view.setTag(holder);
        return view;
    }

    public Spannable getContentSummary(MyTextMessage data) {
        return null;
    }

    public Spannable getContentSummary(Context context, MyTextMessage data) {
        if (data == null) {
            return null;
        } else {
            String content = data.getContent();
            if (content != null) {
                if (content.length() > 100) {
                    content = content.substring(0, 100);
                }

                return new SpannableString(AndroidEmoji.ensure(content));
            } else {
                return null;
            }
        }
    }

    public void onItemClick(View view, int position, MyTextMessage content, UIMessage message) {
    }

    public SpannableStringBuilder getTextMessageContent(Message message) {
//        if (this.textMessageContent == null) {
            MessageContent content = message.getContent();
            if (content instanceof MyTextMessage) {
                MyTextMessage textMessage = (MyTextMessage)content;
                if (textMessage.getContent() != null) {
                    SpannableStringBuilder spannable = new SpannableStringBuilder(textMessage.getContent());
                    AndroidEmoji.ensure(spannable);
                    this.setTextMessageContent(spannable);
                }
            }
//        }

        return this.textMessageContent;
    }

    public void setTextMessageContent(SpannableStringBuilder textMessageContent) {
        this.textMessageContent = textMessageContent;
    }


    public void bindView(final View v, int position, MyTextMessage content, final UIMessage data) {
        CustomizeMessage.ViewHolder holder = (CustomizeMessage.ViewHolder)v.getTag();
        if (data.getMessageDirection() == Message.MessageDirection.SEND) {

            holder.message.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_left);

        } else {
            holder.message.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_right);
        }

        final AutoLinkTextView textView = holder.message;

        if (getTextMessageContent(data.getMessage()) != null) {
            int len = getTextMessageContent(data.getMessage()).length();
            if (v.getHandler() != null && len > 500) {
                v.getHandler().postDelayed(new Runnable() {
                    public void run() {
                        textView.setText(getTextMessageContent(data.getMessage()));
                    }
                }, 50L);
            } else {
                textView.setText(getTextMessageContent(data.getMessage()));

            }
        }

        holder.message.setMovementMethod(new LinkTextViewMovementMethod(new ILinkClickListener() {
            public boolean onLinkClick(String link) {
                RongIM.ConversationBehaviorListener listener = RongContext.getInstance().getConversationBehaviorListener();
                RongIM.ConversationClickListener clickListener = RongContext.getInstance().getConversationClickListener();
                boolean result = false;
                if (listener != null) {
                    result = listener.onMessageLinkClick(v.getContext(), link);
                } else if (clickListener != null) {
                    result = clickListener.onMessageLinkClick(v.getContext(), link, data.getMessage());
                }

                if (listener == null && clickListener == null || !result) {
                    String str = link.toLowerCase();
                    if (str.startsWith("http") || str.startsWith("https")) {
                        Intent intent = new Intent("io.rong.imkit.intent.action.webview");
                        intent.setPackage(v.getContext().getPackageName());
                        intent.putExtra("url", link);
                        v.getContext().startActivity(intent);
                        result = true;
                    }
                }

                return result;
            }
        }));
    }

    private static class ViewHolder {
        AutoLinkTextView message;
        boolean longClick;

        private ViewHolder() {
        }
    }

}
